package com.ycl.bookstore.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.bookstore.common.Result;
import com.ycl.bookstore.pojo.User;
import com.ycl.bookstore.pojo.UserOrder;
import com.ycl.bookstore.service.UserOrderService;
import com.ycl.bookstore.mapper.UserOrderMapper;
import com.ycl.bookstore.utils.MyThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.ycl.bookstore.utils.RedisConstants.USER_ORDER_KEY;

/**
 *
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder>
        implements UserOrderService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private UserOrderMapper orderMapper;

    /**
     * 得到用户订单列表
     *
     * @param pageNo
     * @param pageSize
     * @param orderId
     * @return
     */
    @Override
    public Result getUserOrderList(Long pageNo, Long pageSize, Long orderId) {
        // 获取用户id
        Long userId = MyThreadLocal.getUser().getId();
        // 获取key
        String key = USER_ORDER_KEY + userId;
        // 判断oderId是否为空
        if (orderId != null) {
            Set<String> userOrderSet = stringRedisTemplate.opsForZSet().rangeByScore(key, orderId, orderId);
            return getData(userOrderSet);
        }
        // 起始下标
        Long start = (pageNo - 1) * pageSize;
        // 终止下标
        Long edd = start + pageSize - 1;
        Set<String> userOrderSet = stringRedisTemplate.opsForZSet().range(key, start, edd);
        return getData(userOrderSet);
    }

    /**
     * 删除用户订单消息
     * @param orderId
     * @return
     */
    @Override
    @Transactional
    public Result deleteUserOrder(Long orderId) {
        // 获取用户id
        Long userId = MyThreadLocal.getUser().getId();
        // 获取key
        String key=USER_ORDER_KEY+userId;
        //修改数据库中的消息
        UpdateWrapper<UserOrder> wrapper=new UpdateWrapper<>();
        wrapper.eq("order_id",orderId)
                .set("status",1);
        int updateCount = orderMapper.update(null, wrapper);
        if(updateCount<1){
            return Result.fail("删除失败");
        }
        // 删除redis中的缓存
        Long count = stringRedisTemplate.opsForZSet().removeRangeByScore(key, orderId, orderId);
        if(count<1){
            return Result.fail("删除失败");
        }
        return Result.success();
    }

    /**
     * 得到用户订单列表
     *
     * @return
     */
    public Result getData(Set<String> userOrderSet) {
        Map<String, Object> data = new HashMap<>();
        List<UserOrder> userOrderList = new LinkedList<>();
        if (userOrderSet == null) {
            return Result.success();
        }
        for (String s : userOrderSet) {
            // 将s转化为UserOrder对象
            UserOrder userOrder = JSONUtil.toBean(s, UserOrder.class);
            userOrderList.add(userOrder);
        }
        data.put("total", userOrderSet.size());
        data.put("userOrderList", userOrderList);
        return Result.success(data);
    }
}




